<?php
namespace Home\Controller;
use Lib\Controller;
class BbsController extends Controller{
	/* protected $type = array(
			'TEXT'		=> 1,
			'IMG'		=> 2,
			'AUDIO'		=> 3,
			'VOIDEO'	=> 4
	); */
	/**
	 * 发表一条朋友圈
	 * 支持的类型'TEXT','IMG','AUDIO','VOIDEO'
	 */
	public function put(){
		$content = json_decode($this->param['content'],true);//数据必须为合法的JSON格式
		if(empty($this->param['type']) || empty($content)){
			$this->error(ERROR_NO_ARGV,'参数错误');
		}
		
		//
		$type = 0;
		switch ($this->param['type)']){
			case 'TEXT':
				if(empty($content['content'])){
					$this->error(ERROR_NO_ARGV,'参数错误');
				}
				$type = 1;
				break;
			case 'IMG':
				if(empty($content['title']) || !is_array($content['url'])){
					$this->error(ERROR_NO_ARGV,'参数错误');
				}
				$type = 2;
				break;
			case 'AUDIO':
				if(empty($content['title']) || empty($content['url']) || empty($content['img'])){
					$this->error(ERROR_NO_ARGV,'参数错误');
				}
				$type = 3;
				break;
			case 'VOIDEO':
				if(empty($content['title']) || empty($content['url']) || empty($content['img'])){
					$this->error(ERROR_NO_ARGV,'参数错误');
				}
				$type = 4;
				break;
			default:
				$this->error(ERROR_NO_ARGV,'参数错误');
				break;
		}
		//数据验证通过
		$b = D('BbsContent');
		$cd = array(
				'user_id'		=>	$this->userInfo['user_id'],
				'content'		=>	$this->param['content'],
				'create_time'	=>	microtime(true),
				'type'			=>	$type
		);
		if($b->create($cd)){
			$a = $b->add();
			if($a){
				$this->error(ERROR_SQL_FAILING,'数据库操作失败 ' . $b->getError());
			}else{
				$this->show('操作成功');
			}
		}else{
			$this->error(ERROR_NO_ARGV,'数据不合法');
		}
	}
	/**
	 * 删除一条朋友圈
	 * 只可删除当前登录用户当前用户的朋友圈
	 */
	public function delete(){
		if(empty($this->param['content_id'])){
			$this->error(ERROR_NO_ARGV,'参数错误');
		}
		//验证通过
		$b = D('BbsContent');
		//启动事务，删除朋友圈时，同时删除评论及赞
		$b->startTrans();
		//删除朋友圈
		$r = $b->where(array('user_id'=>$this->userInfo['user_id'],'content_id'=>$this->param['content_id']))->delete();
		
		//删除评论
		$cm = D('BbsComment');
		$rcm = $cm->where(array('content_id'=>$this->param['content_id']))->delete();
		if(empty($r) ||empty($rcm) ){
			$b->rollback();
			$this->error(ERROR_SQL_FAILING,'没有对应的信息或数据库错误 : ' . $b->getError());
		}else{
			$b->commit();
			$this->show('操作成功');
		}
	}
	/**
	 * 评论朋友的朋友圈
	 * 只有当前用户是对方好友，才可进行评论
	 */
	public function comment(){
		if(empty($this->param['content_id']) || empty($this->param['comment'])){
			$this->error(ERROR_NO_ARGV,'参数错误');
		}
		//验证通过
		//检测是否有这么一条朋友圈		
		$b = D('BbsContent');
		$rb = $b->where(array('content_id'=>$this->param['content_id']))->find();
		if(empty($rb)){
			$this->error(ERROR_NO_ARGV,'参数错误，没有此条朋友圈');
		}
		//检测当前用户是否是对方好友，只有对方好友才可以进行评论
		$f = D('Friends');
		$rf = $f->where(array('user_id'=>$rb['user_id'],'friend_id'=>$this->userInfo['user_id']))->select();
		if(empty($rf)){
			$this->error(ERROR_NO_PERMISSIONS,'不是对方好友，不能进行此操作');
		}
		
		//关系验证通过
		//构造数据
		$data = array(
			'user_id'		=>	$this->userInfo['user_id'],
			'comment'		=>	$this->param['comment'],
			'comment_time'	=>	microtime(true),//获取当前时间
			'content_id'	=>	$this->param['content_id'],
			'type'			=>	1
		);
		$cm = D('BbsComment');
		if($cm->create($data)){
			$a = $cm->add();
			if(empty($a)){
				$this->error(ERROR_SQL_FAILING,'数据库操作错误 ：' . $cm->getError());
			}else{
				$this->show(array('msg'=>'操作成功','id'=>$a));
			}
		}else{
			$this->error(ERROR_NO_ARGV,'参数错误');
		}
	}
	/**
	 * 删除一条别人对当前用户的评论
	 */
	public function deleteComment(){
		if(empty($this->param['comment_id'])){
			$this->error(ERROR_NO_ARGV,'参数错误');
		}
		//检测此条评论是否是好友对当前用户的评论
		$cm = D('BbsComment');
		$b  = D('BbsContent');
		//取出评论中的contentid
		$cmd = $cm->where(array('comment_id'=>$this->param['comment_id']))->find();
		if(empty($cmd)){
			$this->error(ERROR_NO_ARGV,'没有这样一条评论');
		}
		//验证是否此条评论是对当前用户朋友圈的评论，如果不是，将不进行删除操作
		$bd = $b->where(array('user_id'=>$this->userInfo['user_id'],'content_id'=>$cmd['content_id']))->find();
		if(empty($bd)){
			$this->error(ERROR_NO_PERMISSIONS,'没有权限，只能删除别人对自己的评论');
		}
		
		//删除评论
		$rcm = $cm->where(array(array('comment_id'=>$this->param['comment_id'])))->delete();
		if(empty($rcm)){
			$this->error(ERROR_SQL_FAILING,'数据库错误 ： ' . $cm->getError());
		}
		$this->show('操作成功');
	}
	/**
	 * 撤回当前用户对别人的评论,删除当前用户对好友的评论
	 */
	public function revokeComment(){
		if(empty($this->param['comment_id'])){
			$this->error(ERROR_NO_ARGV,'参数错误');
		}
		//验证通过
		$cm = D('BbsComment');
		$rcm = $cm->where(array('user_id'=>$this->userInfo['user_id'],'comment_id'=>$this->param['comment_id']))->delete();
		if(empty($rcm)){
			$this->error(ERROR_SQL_FAILING,'数据库错误：'.$cm->getError());
		}
		$this->show('操作成功');
		
	}
	/**
	 * 赞一下别人的朋友圈
	 */
	public function like(){
		if(empty($this->param['content_id']) || empty($this->param['comment'])){
			$this->error(ERROR_NO_ARGV,'参数错误');
		}
		//验证通过
		//检测是否有这么一条朋友圈
		$b = D('BbsContent');
		$rb = $b->where(array('content_id'=>$this->param['content_id']))->find();
		if(empty($rb)){
			$this->error(ERROR_NO_ARGV,'参数错误，没有此条朋友圈');
		}
		//检测当前用户是否是对方好友，只有对方好友才可以进行评论
		$f = D('Friends');
		$rf = $f->where(array('user_id'=>$rb['user_id'],'friend_id'=>$this->userInfo['user_id']))->select();
		if(empty($rf)){
			$this->error(ERROR_NO_PERMISSIONS,'不是对方好友，不能进行此操作');
		}
		
		//关系验证通过
		//构造数据
		$data = array(
				'user_id'		=>	$this->userInfo['user_id'],
				'comment'		=>	'like',
				'comment_time'	=>	microtime(true),//获取当前时间
				'content_id'	=>	$this->param['content_id'],
				'type'			=>	2
		);
		$cm = D('BbsComment');
		if($cm->create($data)){
			$a = $cm->add();
			if(empty($a)){
				$this->error(ERROR_SQL_FAILING,'数据库操作错误 ：' . $cm->getError());
			}else{
				$this->show(array('msg'=>'操作成功','id'=>$a));
			}
		}else{
			$this->error(ERROR_NO_ARGV,'参数错误');
		}
	}
	/**
	 * 撤回对别人的赞
	 */
	public function revokeLike(){
		if(empty($this->param['comment_id'])){
			$this->error(ERROR_NO_ARGV,'参数错误');
		}
		//验证通过
		$cm = D('BbsComment');
		$rcm = $cm->where(array('user_id'=>$this->userInfo['user_id'],'comment_id'=>$this->param['comment_id']))->delete();
		if(empty($rcm)){
			$this->error(ERROR_SQL_FAILING,'数据库错误：'.$cm->getError());
		}
		$this->show('操作成功');
	}
}

?>